package com.handy.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileTool {

	/**
	 * 指定输入流，将内容写入字符串中。
	 * @param is 输入流
	 * @param charset 字符集
	 * @return
	 */
	public static String readFileToString(InputStream is,String charset){
		if (is == null) {
			log.error("文件不存在！");
			return "";
		}
		if (charset == null || "".equals(charset)) charset = "utf-8";
		StringBuffer sb = new StringBuffer();
		String str = "";
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(
					is, charset));
			while ((str = br.readLine()) != null) {
				sb.append(str).append("\r\n");
			}
		} catch (IOException e) {
			try {
				if (is != null)
					is.close();
				if (br != null)
					br.close();
			} catch (IOException e1) {
				log.error(e1.getMessage());
			}
			log.error(e.getMessage());
		}
		return sb.toString();
	}
	
	public static String readFileToString(InputStream is){
		return readFileToString(is,null);
	}
	
	/**
	 * 指定文件名(包括绝对路径)，将内容写入字符串中。
	 * @param fileName 文件名(包括路径) 
	 * @param charset 字符集
	 * @return
	 */
	public static String readFileToString(String fileName,String charset) {
		InputStream is = null;
		try {
			is = new FileInputStream(fileName);
			return readFileToString(is,charset);
		} catch (FileNotFoundException e) {
			log.error(e.getMessage());
			return "";
		}
	}
	
	public static String readFileToString(String fileName) {
		return readFileToString(fileName,null);
	}

	/**
	 * 将内容写入指定的文件中。
	 * @param fileName 文件名(包括路径)
	 * @param content 要写入的内容
	 * @param charset 字符集
	 */
	public static void writeStringToFile(String fileName, String content, String charset) {
		File f = new File(fileName);
		if (f.exists())
			f.delete();
		PrintWriter out = null;
		if (charset == null || "".equals(charset)) charset = "utf-8";
		try {
			out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(fileName), charset)));
			out.write(content);
		} catch (IOException e) {
			log.error(e.getMessage());
		} finally {
			if (out != null)
				out.close();
		}
	}
	
	/**
	 * 将内容写入指定的文件中。
	 * @param fileName 文件名(包括路径)
	 * @param content 要写入的内容
	 */
	public static void writeStringToFile(String fileName, String content) {
		writeStringToFile(fileName, content, null);
	}

	public static int findFileSize(String filename, int unit) {
		int size = 0;
		File f = new File(filename);
		if (f.exists()) {
			FileInputStream fis = null;

			try {
				fis = new FileInputStream(f);
				size = fis.available();
				switch (unit) {
				case KBYTE:
					size = size / 1024;
					break;
				case MBYTE:
					size = size / 1024 / 1024;
					break;
				}
			} catch (FileNotFoundException e) {
				log.error(e.getMessage());
			} catch (IOException e) {
				log.error(e.getMessage());
			}
		}

		return size;
	}

	/**
	 * 创建多级目录
	 * 
	 * @param path
	 * @return
	 */
	public static boolean creatDirs(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return file.mkdirs();
		} else {
			return true;
		}
	}
	
	private static List<String> listFilePath = new ArrayList<String>();
	public static List<String> listFile(String rp) {
		File file = new File(rp);
		File list[] = file.listFiles();
		for (int i = 0; i < list.length; i++) {
			try {
				if (list[i].isDirectory()) {
					FileTool.listFile(list[i].toString());
				} else {
					listFilePath.add(list[i].getAbsolutePath());
				}
			} catch (Exception ex) {
				log.error("Access deny：" + list[i].getAbsolutePath());
			}
		}
		return listFilePath;
	}

	private static Log log = LogFactory.getLog(FileTool.class);

	public static final int BYTE = 0;
	public static final int KBYTE = 1;
	public static final int MBYTE = 2;
}
